package com.hariro.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.qianfeng.project.MainActivity;
import com.qianfeng.project.R;

public class Welcome_Activity extends Activity {

	private ImageView imageView;
	private Handler handler;
	private Animation animation = null;
	private Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		// 初始化UI控件;
		initView();

		// 播放动画;
		playAnim();
	}

	private void playAnim() {
		// 延迟2秒后加载动画
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(2000);
					handler.sendEmptyMessage(0);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

		handler = new Handler() {
			// 开始加载动画
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
					animation = AnimationUtils.loadAnimation(context,
							R.anim.anim_welcome);
					imageView.startAnimation(animation);
					break;
				}
			}
		};

		// 延迟4秒后,Activity跳转到主UI界面.
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(4000);
					Intent intent = new Intent();
					intent.setClass(context, MainActivity.class);
					startActivity(intent);
					finish();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	private void initView() {
		// TODO Auto-generated method stub
		imageView = (ImageView) findViewById(R.id.imageView_welcome_splash);
	}
}